package com.swedne.test.udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;

/**
 * 组播需要加入一个组播地址，需要特定端口来接收数据
 * 1.加入的组播ip不一样，是不能收到组播的
 * 2.加入的组播ip一样，组播port不一样，是不能收到组播的
 * 3.加入的组播ip一样，组播port一样，可以收到发送的组播数据，就算自己发送的数据也能收到
 */

public class UpdGroupClient {
    private int MessageIndex = 0;
    private String ip = "238.9.9.1";//组播地址
    private int port = 8400;//指定数据接收端口
    private boolean closed = false;
    private static final String alarmManager = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
            /*消防联动*/
            "<event>\n" +
            "\t<broadcast_url>/access/forceopen</broadcast_url>\n" +
            "\t<io>1</io>\n" +
            "\t<data>1</data>\n" +
            "\t<active>broadcast_data</active>\n" +
            "\t<type>req</type>\n" +
            "</event>";
/*
            "<event>\n" +
            "\t<broadcast_url>/access/forceopen</broadcast_url>\n" +
            "\t<data>\n" +
            "\t\t<io>0</io>\n" +
            "\t\t<type>2</type>\n" +
            "\t\t<sensor>0</sensor>\n" +
            "\t</data>\n" +
            "\t<talk>\n" +
            "\t\t<build>1</build>\n" +
            "\t\t<unit>2</unit>\n" +
            "\t\t<floor>11</floor>\n" +
            "\t\t<family>11</family>\n" +
            "\t</talk>\n" +
            "\t<active>broadcast_data</active>\n" +
            "\t<type>req</type>\n" +
            "</event>";
*/

    public void start() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("UpdGroupClient start ");
                runClient();
            }
        }).start();
    }

    MulticastSocket socket = null;

    public void runClient() {
        try {
            byte[] receiveBuffer = new byte[1024];
            DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
            InetAddress group = InetAddress.getByName(ip);
            socket = new MulticastSocket(port);
            socket.joinGroup(group);//加入组播地址
            while (!closed) {
                send();
                socket.receive(receivePacket);
                System.out.println("received packet from " + receivePacket.getAddress().getHostAddress() + " : " + receivePacket.getPort());
                String msg = new String(receivePacket.getData(), receivePacket.getOffset(), receivePacket.getLength());
                System.out.println("received " + msg);
                Thread.sleep(2000);
            }
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 组播发送数据
     */
    public void send() {
        String msg = alarmManager;
        try {
            byte[] message = msg.getBytes(); //发送信息
            InetAddress inetAddress = InetAddress.getByName(ip); //指定组播地址
            DatagramPacket datagramPacket = new DatagramPacket(message, message.length, inetAddress, port); //发送数据包囊
            MulticastSocket multicastSocket = new MulticastSocket();//创建组播socket
            multicastSocket.send(datagramPacket);
            System.out.println("send success =============================");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (MessageIndex >= 50) {
            closed = true;
        }
    }


    public static void main(String[] s) {
        UpdGroupClient client = new UpdGroupClient();
        client.start();
    }
}